package com.supos.progress.controller;

import com.alibaba.fastjson.JSONObject;
import com.supos.progress.entity.Result;
import com.supos.progress.util.FileUtil;
import com.supos.progress.util.FormatResultUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @RequestMapping(value = "/excel",method = RequestMethod.POST)
    public Result uploadExcel(@RequestParam("file")MultipartFile file){
        String fileName = file.getOriginalFilename();
        Result result = new Result();
        try {
            FileUtil.uploadFile(file.getBytes(),fileName);
        } catch (IOException e) {
            result.setErrcode(-101);
            result.setMessage("upload fail");
            return result;
        }


        return FormatResultUtil.formatResult();
    }

    @RequestMapping(value = "/images",method = RequestMethod.POST)
    public Result uploadImages(HttpServletRequest request){
        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
        Result result = new Result();
        try {
            FileUtil.uploadMultiFile(files);
        } catch (IOException e) {
            result.setErrcode(-101);
            result.setMessage("upload fail");
            return result;
        }
        return FormatResultUtil.formatResult();
    }
}
